package com.apes.fn.ssm.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apes.fn.ssm.repository.StockCalcuResultRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service("calcuInterfaceDownService")
public class CalcuInterfaceDownService /*extends DomainService*/ {
    @Autowired
    private StockCalcuResultRepository stockCalcuResultRepository;

    // 下载明细 - 测算结果
    public JSONObject downCalcuItem(SimpleRequest request) {
        Map getMap = request.getMap();
        int page = (Integer) getMap.get("page");
        int size = (Integer) getMap.get("size");
        int minPage = page * size;
        int maxPage = minPage + size;
        List<Map> calcuResults = stockCalcuResultRepository.downCalcuItem(maxPage, minPage);
        int calcuResultSize = calcuResults.size();
        JSONObject returnData = new JSONObject();
        if (calcuResultSize != 0) {
            JSONObject content = new JSONObject();
            content.put("content", (JSONArray) JSON.toJSON(calcuResults));
            returnData.put("body", content);
            returnData.put("total", calcuResultSize);
            returnData.put("pages", Math.ceil(calcuResultSize / size));
        }
        return returnData;
    }

    public JSONObject categoryAnalyze(SimpleRequest request) {
        Map getMap = request.getMap();
        int page = (Integer) getMap.get("page");
        int size = (Integer) getMap.get("size");
        int minPage = page * size;
        int maxPage = minPage + size;

        List<Map> getCategoryAnalyze = stockCalcuResultRepository.downCategoryAnalyze(maxPage, minPage);
        int CategoryAnalyzeSize = getCategoryAnalyze.size();
        JSONObject returnData = new JSONObject();
        if (CategoryAnalyzeSize != 0) {
            JSONObject content = new JSONObject();
            content.put("content", (JSONArray) JSON.toJSON(getCategoryAnalyze));
            returnData.put("body", content);
            returnData.put("total", CategoryAnalyzeSize);
            returnData.put("pages", Math.ceil(CategoryAnalyzeSize / size));
        }
        return returnData;
    }

    public JSONObject brandAnalyze(SimpleRequest request) {
        Map getMap = request.getMap();
        int page = (Integer) getMap.get("page");
        int size = (Integer) getMap.get("size");
        int minPage = page * size;
        int maxPage = minPage + size;

        List<Map> getBrandAnalyze = stockCalcuResultRepository.downBrandAnalyze(maxPage, minPage);
        int brandAnalyzeSize = getBrandAnalyze.size();
        JSONObject returnData = new JSONObject();
        if (brandAnalyzeSize != 0) {
            JSONObject content = new JSONObject();
            content.put("content", (JSONArray) JSON.toJSON(getBrandAnalyze));
            returnData.put("body", content);
            returnData.put("total", brandAnalyzeSize);
            returnData.put("pages", Math.ceil(brandAnalyzeSize / size));
        }
        return returnData;
    }

}
